package com.project.web.system.controller.app;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.project.common.core.utils.exception.CustomException;
import com.project.common.core.utils.exception.RESP_CODE_ENUM;
import com.project.common.core.utils.exception.Result;
import com.project.common.core.utils.redis.RedisClient;
import com.project.feign.client.BusinessServiceClient;
import com.project.feign.client.OrderServiceClient;
import com.project.web.system.controller.common.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * 控制器编码范例
 *
 * @author abc
 * @date 2019-08-20 19:57
 */
@Api(tags = "编码范例")
@RestController("SystemServiceDemoController")
@RequestMapping("/system/demo")
public class DemoController extends BaseController {

    @Resource(name = "redisClient")
    private RedisClient redisClient;

    @Resource
    private OrderServiceClient orderServiceClient;

    @Resource
    private BusinessServiceClient businessServiceClient;
    /**
     * 其他服务接口
     *
     * @param test 测试参数
     * @return
     */
    @ApiOperation(value = "testOrder服务接口")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "test", value = "测试参数", dataType = "String", required = true)
    })
    @GetMapping(value = "/testOrder")
    public Result<String> testOrder(String test) {
        System.out.println(test);
        Result<String> test1 = orderServiceClient.test(test);
        return test1;
    }
    /**
     * 其他服务接口
     *
     * @param test 测试参数
     * @return
     */
    @ApiOperation(value = "testBusiness服务接口")
    @SentinelResource(value = "testBusiness", blockHandler = "exceptionHandler")
    @GetMapping(value = "/testBusiness")
    public Result<Object> testBusiness(String test) {
        System.out.println(test);
        try {
            Result<Object> test1 = businessServiceClient.test();
            return test1;
        } catch (Exception e) {
            return null;
        }

    }

    // Block 异常处理函数，参数最后多一个 BlockException，其余与原函数一致.
    public Result<Object> exceptionHandler(BlockException ex) {
        ex.printStackTrace();
        Result<Object> result = new Result<>();
        addResCodeToMap(RESP_CODE_ENUM.RATE_LIMITER, result);
        return result;
    }

    /**
     * 普通控制器接口书写规范
     *
     * @return
     */
    @ApiOperation(value = "普通接口书写范例")
    @RequestMapping(value = "/updateSysConfig", method = RequestMethod.GET)
    public Result<Map<String, Object>> updateSysConfig() {

        // redis存取值
        redisClient.set("redis.abdddc", 123);
//        redisClient.get("key");

        // controller层异常方式
        if (false) {
            // 必要的地方打印日志【日志使用占位符方式，如果打印堆栈信息，可以使用ExceptionUtils】
            log.info("此处有错误:{}", "错误信息");

            // 如果需要返回错误提示【框架中错误返回均以全局异常处理】
            throw new CustomException(RESP_CODE_ENUM.ILLEGAL_PARAMETER);
        }

        // 如果返回对象信息，可以为简单类型、复合类型[Object、Map、List、model实体等]
        Map<String, Object> returnMap = new HashMap<>();
        returnMap.put("name", "小栗子");
        returnMap.put("集合", new ArrayList<String>());
        returnMap.put("数字", 1231312L);
        return getResult(returnMap);
    }


}
